package com.etoak.filter;

import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.etoak.common.core.vo.ResultVO;
import com.etoak.common.jwt.MyJwtUtil;
import com.etoak.properties.WhiteListProperties;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class TokenFilter implements GlobalFilter {

    @Autowired
    WhiteListProperties whiteListProperties;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        //请求地址
        String uri = request.getPath().value();
        log.info("请求地址:{}",uri);
        if(whiteListProperties.getUris().contains(uri)){
            return chain.filter(exchange);
        }
        String token = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
        if(StringUtils.isEmpty(token)){
            return noAuth(response,"请传入令牌");
        }
        try{
            MyJwtUtil.parse(token);
        }catch(ExpiredJwtException e){
            return  noAuth(response,"令牌已经过期");
        }catch (Exception e){
            return noAuth(response,"令牌错误");
        }
        return chain.filter(exchange);


    }

    private Mono<Void> noAuth(ServerHttpResponse response, String message) {
        response.getHeaders().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8");
        ResultVO<Object> resultVO = ResultVO.failed(403, message);
        String jsonStr = JSONUtil.toJsonStr(resultVO, JSONConfig.create().setIgnoreNullValue(false));
        DataBuffer dataBuffer = response.bufferFactory().wrap(jsonStr.getBytes());
        return response.writeWith(Mono.just(dataBuffer));
    }
}
